package com.android.hedgehog;

import java.util.List;

import android.graphics.Color;
import android.util.Log;
import com.android.hedgehog.framework.Game;
import com.android.hedgehog.framework.Graphics;
import com.android.hedgehog.framework.Screen;
import com.android.hedgehog.framework.Input.TouchEvent;
import com.android.hedgehog.puzzles.DecipherScreen;
import com.android.hedgehog.puzzles.SeesawScreen;

/**
 * 
 */
public class FreePlayScreen extends Screen {
	enum GameStatus { // the game is always in one of these states
		Ready, Running, Paused, GameOver
	}
	public int totalGameTime = 195; // # of seconds the game will run
	public double currentTime = 0.0; // total elapsed time so far
	public int totalScore = 0; // total points by player
	public Graphics g; // use g in order to draw to the screen
	public List<TouchEvent> touchEvents; // needed to handle touch inputs
	public int len; // needed to handle touch inputs
	GameStatus state = GameStatus.Ready; // game starts in "Ready" state
	GameScreen myGame;
	int gameNumber;
	String[] gameTitles = {"CrazyBoxes", "TripleSum", "DataMatrix","Racer","DecipherScreen","SeeSaw"};
	TouchEvent event;

	public FreePlayScreen(Game game, int gameNumber) {
		super(game);
		this.gameNumber = gameNumber;
		g = game.getGraphics();
	}

	public void createGame() {
		if (gameNumber == 0)
			myGame = new SampleGameScreen(g, (int) ((Math.random() * 10) + 1),
					currentTime);
		if (gameNumber == 2)
			myGame = new DataMatrixScreen(g, (int) ((Math.random() * 10) + 1),
					currentTime);
		if (gameNumber == 3)
			myGame = new RacerScreen(g, (int) ((Math.random() * 10) + 1),
					currentTime);
		if(gameNumber==4)
			myGame=new DecipherScreen(g, (int) ((Math.random() * 10) + 1));
		if(gameNumber==5)
			myGame = new SeesawScreen(g,(int) ((Math.random() * 10) + 1));
			
	}

	@Override
	public void update(float deltaTime) {
		if (state == GameStatus.Ready) {
			createGame();
			state = GameStatus.Running;
		}

		if (state == GameStatus.Running) {
			currentTime += deltaTime;
			totalScore = myGame.update(-1001, 1001, totalScore, currentTime);
		}

		if (currentTime > totalGameTime) {
			game.setScreen(new FreePlayResult(game,gameNumber, totalScore));
		}

		if (myGame.gameOver) {// restart
			state = GameStatus.Ready;
		}

		touchEvents = game.getInput().getTouchEvents();
		len = touchEvents.size();

		for (int i = 0; i < len; i++) {
			event = touchEvents.get(i);

			if (event.type == TouchEvent.TOUCH_UP) {

				if (state == GameStatus.Running) {// RUNNING
					if (event.x < 30 && event.y < 30) { //Go to Pause Screen
						state = GameStatus.Paused;  
						if (Settings.soundEnabled)
							Assets.click.play(1);
						return;
					}
					if (event.x > 290 && event.y < 30) { //go restart game
						game.setScreen(new FreePlayScreen(game,gameNumber));
						if (Settings.soundEnabled)
							Assets.click.play(1);
						return;
					}
					totalScore = myGame.update(event.x, event.y, totalScore,
							currentTime);
				} // //////////////////////////////// RUNNING

				if (state == GameStatus.Paused) {// PAUSED
					if (event.y > 135 && event.y < 185) {
						state = GameStatus.Running;
						if (Settings.soundEnabled)
							Assets.click.play(1);
						return;
					}
					if (event.x > 70 && event.x < 245 && event.y > 255
							&& event.y < 305) {
						game.setScreen(new MainMenu(game));
						if (Settings.soundEnabled)
							Assets.click.play(1);
						return;
					}
				}// ////////////////////////////// PAUSED
			}
		}

	}
	@Override
	public void present(float deltaTime) {
		g.drawPixmap(Assets.blackbackground, 0, 0);

		if (state == GameStatus.Running) {
			g.drawText("" + totalScore, 60, 20, 20, Color.WHITE); // score
			g.drawText("" + (int) (totalGameTime - currentTime), 260, 20, 20,
					Color.WHITE); // time.
			g.drawPixmap(Assets.whitebuttons, 0, 0, 0, 0, 30, 30);// pause
			g.drawPixmap(Assets.whitebuttons, 290, 0, 30, 0, 30, 30);// restart
			g.drawSpecialText(gameTitles[gameNumber], 160, 20, 20, 1,Color.MAGENTA);
			g.drawLine(0, 30, 320, 30, Color.WHITE);
			myGame.present();
		}

		if (state == GameStatus.Paused) {
			g.drawSpecialText("RESUME", 160, 180, 80, 2,Color.GREEN);
			g.drawSpecialText("QUIT", 160, 300, 80, 2,Color.RED);
		}

	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub

	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub

	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}

}
